<?php

class ErrorController extends Zend_Controller_Action
{

    public function errorAction()
    {
        $errors = $this->_getParam('error_handler');
        
        $this->view->headTitle('An error occurred');
        
        $previous_request=$errors->request;

        $this->_logger = Zend_Registry::get('loggerTxt');
        
        $tipo=$this->_getError($errors->exception->getCode());
        $this->view->tipo=$tipo;
       

        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
        
                // 404 error -- controller or action not found
                if(!$previous_request->isXmlHttpRequest()){
                    
                    $this->_logger->err('404 '. $errors->exception->getMessage(). ' '.$previous_request->getControllerName().':'.$previous_request->getActionName());
                    $this->getResponse()->setHttpResponseCode(404);
                }else{
                    $this->_logger->err('Ajax 404 '. $errors->exception->getMessage(). ' '.$previous_request->getControllerName().':'.$previous_request->getActionName());
                    $this->_helper->layout->disableLayout();
                    $this->getResponse()->setHttpResponseCode(200);
                }
                
                
                $this->view->message = 'Page not found';

                break;
            default:
                // application error
                
                if(!$previous_request->isXmlHttpRequest()){
                    
                    
                    if(!$tipo) $this->_logger->err('500 '.$errors->exception->getMessage(). ' '.$previous_request->getControllerName().':'.$previous_request->getActionName());
 
                    $this->getResponse()->setHttpResponseCode(500);
                }else{
                    if(!$tipo) $this->_logger->err('Ajax 500 '.$errors->exception->getMessage().' '.$previous_request->getControllerName().':'.$previous_request->getActionName());
                   
                    $this->_helper->layout->disableLayout();
     
                    $this->getResponse()->setHttpResponseCode(200);
                }

                $this->view->message = 'Application error';
                break;
        }
        

        // conditionally display exceptions
        if ($this->getInvokeArg('displayExceptions') == true) {
            $this->view->exception = $errors->exception;
        }
        
        $this->view->request   = $errors->request;
    }

    public function deniedAction()
    {
        $this->_logger->err('Acceso Denegado. '.$previous_request->getControllerName().':'.$previous_request->getActionName());
                   
    }
	
    protected  function _getError($errno)
    {
       switch($errno){
            case 1:
                return 'E_ERROR';
            case 2:
                return 'E_WARNING';
            case 4:
                return 'E_PARSE';
            case 8:
                return 'E_NOTICE';
            case 16:
                return 'E_CORE_ERROR';
            case 32:
                return 'E_CORE_WARNING ';
            case 64:
                return 'E_COMPILE_ERROR ';
            case 128:
                return 'E_COMPILE_WARNING';
            case 256:
                return 'E_USER_ERROR';
            case 512:
                return 'E_USER_WARNING';
            case 1024:
                return 'E_USER_NOTICE ';
            case 2048:
                return 'E_STRICT ';
            case 4096:
                return 'E_RECOVERABLE_ERROR';
            case 8192:
                return 'E_DEPRECATED';
            case 16384:
                return 'E_USER_DEPRECATED ';
            case 32767:
                return 'E_ALL';
       }
    }



}
